

namespace Orz.LogManager
{
	//========================================================================
	// 	Imported Namespaces
	//========================================================================
	using System;
	using System.IO;
	using System.Collections;
	using System.Text.RegularExpressions;
	using System.Drawing;
	using Orz.LogManager.Config;
	

	///
	/// <summary>
	/// Filters and colour-codes lines of text.
	/// </summary>
	/// <remarks>
	/// This class can be set up to search for a specific pattern in given 
	/// lines of text and to choose a colour based on a match, perform a substitution,
	/// or both.
	/// </remarks>
	///
	public class LineMatch
	{
		//================================================================
		// 	Private Data Members
		//================================================================
				
		/// <summary>The colour to be used if none is specified</summary>
		private static Color oDefaultColour_c = Color.Empty;

		/// <summary>The settings that this line match is based on</summary>
		private logsetLogfileMatch match_i;
		
		/// <summary>The regular expression engine object to use for matching</summary>
		private Regex oRegex_i = null;
		
		/// <summary>The colour to paint matched lines</summary>
		private Color oColour_i = Color.Empty;
		
		
		
		
		//================================================================
		// 	Constructors
		//================================================================

		///
		/// <summary>
		/// Stores the input parameters and creates a new regular expression
		/// instance ready for use.
		/// </summary>
		///
		public LineMatch (logsetLogfileMatch match_p)
		{
			match_i = match_p;
			RefreshMatchSettings();
		}
		

		
		
		
		//================================================================
		// 	Methods
		//================================================================
		
		/// <summary>
		/// Suggests that the 'logsetLogfileMatch' object that this object is based on has been changed,
		/// and that any compiled regex settings (etc) should be re-compiled.
		/// </summary>
		public void RefreshMatchSettings ()
		{
			oRegex_i = new Regex (match_i.pattern.Value, RegexOptions.IgnoreCase | RegexOptions.Multiline);
			
			String sColourName = match_i.colour;
			
			if (IsEmtpyColourDescription(sColourName))
		    {
		    	oColour_i = Color.Empty;
		    }
		    else
	    	{
		    	oColour_i = Color.FromName(sColourName);
		    }		    
		}
		
		
		
		
		public LineMatchResult DoMatch (String sLine_p)
		{
			LineMatchResult result = new LineMatchResult();
			result.UpdatedText = sLine_p;
			
			bool isMatch = oRegex_i.IsMatch(sLine_p);
			
			if (match_i.pattern.negated) 
			{
				isMatch = !isMatch;
			}
			
			if (isMatch)
			{
				result.Matched = true;
				result.DontPrint = match_i.dontPrint;
				
				if (! match_i.dontPrint)
				{
					result.Colour = oColour_i;
					
					if (null != match_i.replacement)
					{
						result.UpdatedText = oRegex_i.Replace(sLine_p, match_i.replacement);
					}
					else
					{
						result.UpdatedText = sLine_p;
					}
					
					if (match_i.unescapeQuerystring)
					{
						result.UpdatedText = Utils.UnescapeQueryString(sLine_p);
					}
					
					if (match_i.beep)
					{
						System.Media.SoundPlayer playa = new System.Media.SoundPlayer("beep.wav");
						playa.Play();
					}
				}				
			}
			
			return result;
		}
		
		
		
		public logsetLogfileMatch InternalMatchDetails
		{
			get { return match_i; }
		}
		
		
		///
		/// <summary>
		/// Determines if this colour description string represents the 'default' colour.
		/// </summary>
		///
		private bool IsEmtpyColourDescription (string sColour_p)
		{
			return (null == sColour_p  ||  
			        0 == sColour_p.Trim().Length  ||  
			        sColour_p.Equals("Normal")  ||  
			        sColour_p.Equals("Default")  ||  
			        sColour_p.Equals("None"));
		}
		
		
	} // end class
}
	
